package com.quanyan.club.operate.web;

import com.alibaba.fastjson.JSON;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.club.entity.db.TbClub;
import com.quanyan.club.entity.vo.req.movement.ReqClubListInfo;
import com.quanyan.club.mapper.TbClubMapper;
import com.quanyan.club.operate.entity.req.yearactivity.ReqEnergyOrClickEgg;
import com.quanyan.club.operate.entity.resp.yearactivity.RespEnergyHomePage;
import com.quanyan.club.operate.service.ClubYearActivityService;
import com.quanyan.club.operate.service.LotteryService;
import com.quanyan.club.service.ClubMemberService;
import com.quanyan.club.web.BaseController;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.redisCluster.config.RedisTemplate;
import com.quanyan.redisCluster.utils.RedisKey;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by xian-guo on 2016/12/30.
 */
@RestController
@RequestMapping("/api/club/operate/yearactivity")
public class ClubYearActivityController extends BaseController{

    public static final Logger logger = LoggerFactory.getLogger(ClubYearActivityController.class);

    @Autowired
    private ClubYearActivityService clubYearActivityService;

    @Autowired
    private ClubMemberService clubMemberService;

    /**
     * 进砸鸟蛋页面
     * @param request
     * @param paramMap
     * @return
     */
    @RequestMapping(value = "/getEnergyHomePageMessage", method = RequestMethod.POST)
    public APIResponse<RespEnergyHomePage> getEnergyHomePageMessage(HttpServletRequest request, @RequestBody Map<String,Object> paramMap){
        logger.warn("---getEnergyHomePageMessage--->>"+JSON.toJSONString(paramMap));
        Integer uid = getUid(request, true);
        if (!paramMap.containsKey("clubId")){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        return clubYearActivityService.getEnergyHomePageMessage((Integer) paramMap.get("clubId"),uid);
    }

    /**
     * 充能 OR 砸鸟蛋 OR 判断俱乐部成员
     * @param request
     * @param reqEnergyOrClickEgg
     * @return
     */
    @RequestMapping(value = "/chargeEnergyOrClickEgg", method = RequestMethod.POST)
    public APIResponse chargeEnergyOrClickEgg(HttpServletRequest request, @RequestBody ReqEnergyOrClickEgg reqEnergyOrClickEgg){
        logger.warn("---chargeEnergyOrClickEgg--->>"+JSON.toJSONString(reqEnergyOrClickEgg));
        if (null ==reqEnergyOrClickEgg){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        Integer uid = getUid(request, true);
        reqEnergyOrClickEgg.setuId(uid);
        if (2==reqEnergyOrClickEgg.getType()){//判断是否是俱乐部成员
            if(!clubMemberService.isClubMember(uid,reqEnergyOrClickEgg.getClubId())){
                return APIResponse.returnFail("您还不是俱乐部成员，赶紧加入吧！");
            }
            return APIResponse.returnSuccess("是俱乐部成员!");
        }
        return clubYearActivityService.chargeEnergyOrClickEgg(reqEnergyOrClickEgg);
    }

    /**
     * 获取充能成员信息
     * @param request
     * @param reqClubListInfo
     * @return
     */
    @RequestMapping(value = "/getChargeEnergyMember", method = RequestMethod.POST)
    public APIResponse getChargeEnergyMember(HttpServletRequest request, @RequestBody ReqClubListInfo reqClubListInfo){
        logger.warn("---chargeEnergyOrClickEgg--->>"+JSON.toJSONString(reqClubListInfo));
        if (null ==reqClubListInfo || !reqClubListInfo.getParamMap().containsKey("clubId")){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        Integer uid = getUid(request, true);
        return clubYearActivityService.getChargeEnergyMember(reqClubListInfo);
    }
}
